﻿Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class FrmSettings
    Dim SettingsPath As String = Application.StartupPath
    Private Const EM_SETCUEBANNER As Integer = &H1501
    Dim ErrorLog As String = String.Empty
    Dim EmptyTxt As Integer = 0

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
    End Function

    Private Sub SetCueText(ByVal control As Control, ByVal text As String)
        SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
    End Sub

    Private Sub FrmSettings_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If TxtProfile1.Text = String.Empty Then
            EmptyTxt += 1
        End If
        If TxtProfile2.Text = String.Empty Then
            EmptyTxt += 1
        End If
        If TxtFirefox.Text = String.Empty Then
            EmptyTxt += 1
        End If
        If TxtCCleaner.Text = String.Empty Then
            EmptyTxt += 1
        End If
        If EmptyTxt >= 1 Then
            If MsgBox("Are you sure you exit? Some paths are missing", MsgBoxStyle.Question + MsgBoxStyle.YesNo, Title:="Confirmation") = MsgBoxResult.No Then
                e.Cancel = True
                EmptyTxt = 0
            Else
                FrmMain.Show()
            End If
        Else
            FrmMain.Show()
        End If
    End Sub

    Private Sub FrmSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetCueText(TxtProfile1, "Profile 1 pictures path")
        SetCueText(TxtProfile2, "Profile 2 pictures path")
        SetCueText(TxtFirefox, "Firefox path")
        SetCueText(TxtCCleaner, "CCleaner path")
        TxtProfile1.Text = My.Settings.Profile1Path
        TxtProfile2.Text = My.Settings.Profile2Path
        TxtFirefox.Text = My.Settings.FirefoxPath
        TxtCCleaner.Text = My.Settings.CCleanerPath
        If TxtFirefox.Text = String.Empty Then
            If My.Computer.FileSystem.FileExists("C:\Program Files (x86)\Mozilla Firefox\firefox.exe") = True Then
                TxtFirefox.Text = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
                My.Settings.FirefoxPath = TxtFirefox.Text
            Else
                If My.Computer.FileSystem.FileExists("C:\Program Files\Mozilla Firefox\firefox.exe") = True Then
                    TxtFirefox.Text = "C:\Program Files\Mozilla Firefox\firefox.exe"
                    My.Settings.FirefoxPath = TxtFirefox.Text
                End If
            End If
        End If
        If TxtCCleaner.Text = String.Empty Then
            If My.Computer.FileSystem.FileExists("C:\Program Files\CCleaner\CCleaner64.exe") = True Then
                TxtCCleaner.Text = "C:\Program Files\CCleaner\CCleaner64.exe"
                My.Settings.CCleanerPath = TxtCCleaner.Text
            Else
                If My.Computer.FileSystem.FileExists("C:\Program Files\CCleaner\CCleaner.exe") Then
                    TxtCCleaner.Text = "C:\Program Files\CCleaner\CCleaner.exe"
                    My.Settings.CCleanerPath = TxtCCleaner.Text
                End If
            End If
        End If
    End Sub

    Private Sub BtnProfile1_Click(sender As Object, e As EventArgs) Handles BtnProfile1.Click
        Dim Profile1Dialog As New FolderBrowserDialog
        Profile1Dialog.Description = "Profile 1 pictures"
        Profile1Dialog.RootFolder = Environment.SpecialFolder.Desktop
        If Profile1Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TxtProfile1.Text = Profile1Dialog.SelectedPath
        End If
    End Sub

    Private Sub BtnProfile2_Click(sender As Object, e As EventArgs) Handles BtnProfile2.Click
        Dim Profile2Dialog As New FolderBrowserDialog
        Profile2Dialog.Description = "Profile 2 pictures"
        Profile2Dialog.RootFolder = Environment.SpecialFolder.Desktop
        If Profile2Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TxtProfile2.Text = Profile2Dialog.SelectedPath
        End If
    End Sub

    Private Sub BtnFirefox_Click(sender As Object, e As EventArgs) Handles BtnFirefox.Click
        Dim FirefoxDialog As New OpenFileDialog
        FirefoxDialog.Title = "Firefox"
        FirefoxDialog.Multiselect = False
        FirefoxDialog.Filter = "Firefox|*.exe"
        FirefoxDialog.InitialDirectory = Environment.SpecialFolder.Desktop
        If FirefoxDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TxtFirefox.Text = FirefoxDialog.FileName
        End If
    End Sub

    Private Sub BtnCCleaner_Click(sender As Object, e As EventArgs) Handles BtnCCleaner.Click
        Dim CCleanerDialog As New OpenFileDialog
        CCleanerDialog.Title = "CCleaner"
        CCleanerDialog.Multiselect = False
        CCleanerDialog.Filter = "CCleaner|*.exe"
        CCleanerDialog.InitialDirectory = Environment.SpecialFolder.Desktop
        If CCleanerDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TxtCCleaner.Text = CCleanerDialog.FileName
        End If
    End Sub

    Private Sub BtnSaveExit_Click(sender As Object, e As EventArgs) Handles BtnSaveExit.Click
        My.Settings.Profile1Path = TxtProfile1.Text
        My.Settings.Profile2Path = TxtProfile2.Text
        My.Settings.FirefoxPath = TxtFirefox.Text
        My.Settings.CCleanerPath = TxtCCleaner.Text
        ErrorLog = SavingClass.SaveSettings(SettingsPath)
        If Not ErrorLog = "OK" Then
            MsgBox("Error loading settings: " & ErrorLog, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Title:="Error")
            ErrorLog = String.Empty
        End If
        Me.Close()
    End Sub

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        If MsgBox("Are you sure you exit without saving?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, Title:="Confirmation") = MsgBoxResult.Yes Then
            Me.Close()
        End If
    End Sub
End Class